package com.umeox.watch.moto.dataservice.mqtt;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class PayloadMessage implements Parcelable {

    public static final int CODE_SUCCESS = 0;
    public static String CLIENT_RPC_SERVER_RESPONSE_METHOD = "method";
    public static String CLIENT_RPC_SERVER_RESPONSE_RESULT = "result";
    public static String CLIENT_RPC_SERVER_RESPONSE_ERROR_MSG = "msg";

    public static String ERR_CODE = "ERR_CODE";
    public static String ERR_MSG = "ERR_MSG";

    public static final Creator<PayloadMessage> CREATOR = new Creator<PayloadMessage>() {
        @Override
        public PayloadMessage createFromParcel(Parcel source) {
            return new PayloadMessage(source);
        }

        @Override
        public PayloadMessage[] newArray(int size) {
            return new PayloadMessage[size];
        }
    };
    private String topic;
    private HashMap<String, Object> propertys;

    public PayloadMessage(String topic) {
        this.setTopic(topic);
    }

    protected PayloadMessage(Parcel in) {
        this.topic = in.readString();
        this.propertys = (HashMap<String, Object>) in.readSerializable();
    }

    public static PayloadMessage convertAttributesPM(String topic, byte[] payload) {
        PayloadMessage pm = new PayloadMessage(topic);
        String json = new String(payload);
        if (!TextUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            for (Map.Entry entry : jsonObject.entrySet()) {
                String key = (String) entry.getKey();
                pm.setPropertys(key, entry.getValue());
            }
        }
        return pm;
    }

    public static PayloadMessage convertAttributePM(String topic, byte[] payload) {
        PayloadMessage pm = new PayloadMessage(topic);
        String json = new String(payload);
        if (!TextUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (jsonObject.isEmpty()) {
                pm.setPropertys("DEVICE_IS_INIT", false);
            } else {
                for (Map.Entry entry : jsonObject.entrySet()) {
                    String key = (String) entry.getKey();
                    pm.setPropertys(key, entry.getValue());
                }
            }
        }
        return pm;
    }

    /**
     * 转换RPC消息
     * 1.转换服务端Rpc请求内容
     * 2.转换客户端Rrc请求时服务端响应结果
     */
    public static PayloadMessage convertRPCMessagePM(String topic, byte[] payload) {
        PayloadMessage pm = new PayloadMessage(topic);
        String json = new String(payload);
        if (!TextUtils.isEmpty(json)) {
            JSONObject jsonObject = JSONObject.parseObject(json);
            for (Map.Entry entry : jsonObject.entrySet()) {
                String key = (String) entry.getKey();
                pm.setPropertys(key, entry.getValue());
            }
        }
        return pm;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }

    public boolean hasProperty(String name) {
        return getPropertys() != null && getPropertys().containsKey(name);
    }

    public Object getProperty(String name) {
        return getPropertys().get(name);
    }

    public HashMap<String, Object> getPropertys() {
        if (propertys == null) {
            propertys = new HashMap<>();
        }
        return propertys;
    }

    public void setPropertys(String name, Object value) {
        getPropertys().put(name, value);
    }

    @Override
    public String toString() {
        return "PayloadMessage{" +
                "topic='" + topic + '\'' +
                ", propertys=" + propertys +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.topic);
        dest.writeSerializable(this.propertys);
    }
}
